import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-04-26
 * Time: 20:41
 */
class Student {

}
public class Test {

    //找到第一个重复的数据
    public static void main(String[] args) {
        int[] arr = {1,1,2,3,4,4,5,6,7,8,9,9,9,1,3,2,8};
        HashSet<Integer> hashSet = new HashSet<>();
        for (int i = 0; i < arr.length; i++) {
            if(!hashSet.contains(arr[1])) {
                hashSet.add(arr[i]);
            }else {
                System.out.println(arr[i]);
                return;
            }
        }
    }
    public static void main5(String[] args) {
        //去掉重复元素
        int[] arr = {1,1,1,1,8,8,6,6,8,1,9,7,5,5,4,1,8,3};
        HashSet<Integer> hashSet = new HashSet<>();
        for (int i = 0; i < arr.length; i++) {
            hashSet.add(arr[i]);
        }
        System.out.println(hashSet);
    }
    public static void main4(String[] args) {
        //不会放重的元素，如果重复的话会刷新
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("abc", 3);
        hashMap.put("def", 1);
        hashMap.put("ghi", 5);
        hashMap.put("xyz", 8);
        System.out.println(hashMap);

        HashSet<String> hashSet = new HashSet<>();
        hashSet.add("abc");
        hashSet.add("def");
        hashSet.add("ghi");
        hashSet.add("xyz");
        System.out.println(hashSet);

        HashMap<Student, Integer> hashMap2 = new HashMap<>();
        //哈希表是不需要比较的，所以没有重写cmp方法也不会报错
        hashMap2.put(new Student(), 10);
        hashMap2.put(new Student(), 18);

    }

    public static void main3(String[] args) {
        TreeSet<String> set = new TreeSet<>();

        set.add("ab");
        set.add("cd");
        set.add("ef");
        set.add("gh");

        System.out.println(set.contains("ab"));
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }


    public static void main2(String[] args) {
        //底层是红黑树
        TreeMap<String, Integer> treeMap = new TreeMap<>();
        //key value模型
        treeMap.put("hello", 3);
        treeMap.put("word", 4);
        treeMap.put("ujn", 5);

        int val = treeMap.get("word");
       // int val2 = treeMap.get("word2");//会报错 此时用Integer
        int val3 = treeMap.getOrDefault("word2", 6);

        Set<Map.Entry<String, Integer>> entrySet = treeMap.entrySet();
        for(Map.Entry<String, Integer> entry : entrySet) {
            System.out.println("key: " + entry.getKey() + " val: " + entry.getValue());
        }

        //Set<String> set = treeMap.keySet();

        System.out.println(val);
        System.out.println(val3);

        TreeMap<Student, Integer> treeMap2 = new TreeMap<>();
        //没有重写cmp方法，所以会报错
        //treeMap2.put(new Student(), 10);


    }

    public static void main1(String[] args) {
        BinerySearchTree binerySearchTree = new BinerySearchTree();
        binerySearchTree.insert(10);
        binerySearchTree.insert(5);
        binerySearchTree.insert(15);
        binerySearchTree.insert(6);
        binerySearchTree.insert(9);

        BinerySearchTree.TreeNode ret = binerySearchTree.search(15);
        binerySearchTree.remove(15);
        BinerySearchTree.TreeNode ret2 = binerySearchTree.search(15);
        System.out.println(ret.val);
        System.out.println(ret2.val);//空指针异常
    }
}
